package com.ccx.coachsite.ui.own;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ccx.coachsite.CoachSiteMainActivity;
import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.Account;
import com.ccx.coachsite.bus.AccountMgr;
import com.ccx.coachsite.bus.ExpertExBus;
import com.ccx.coachsite.bus.ILoadBus;
import com.ccx.coachsite.bus.LoadDataBus;
import com.ccx.coachsite.bus.MessageInfo;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.bus.UserInfo;
import com.ccx.coachsite.ui.CommonAppView;
import com.ccx.coachsite.ui.login.RegisterActivity;
import com.ccx.coachsite.ui.login.TopViewUtil;
import com.ccx.coachsite.ui.manage.ManageKey;
import com.ccx.coachsite.ui.manage.MyManageView;
import com.ccx.coachsite.util.CommonViewUtils;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;

public class OwerMainBase  extends CommonAppView implements ILoadBus{
	protected FrameLayout rootLayout;
	protected Context mContext;
	protected int currentViewId;
	protected int rootViewId;
	
	protected Button btnRefresh, btnSettings;
	protected View mBtnBack;
	protected View mBtnRight;
	
	protected MyOwerView myOwerView;
	protected MyManageView mMyManageView;
	
	protected OwerSettingView mOwerSettingView;	
	protected OwerMessageView mOwerMessageView;
	
	protected OwerMoreScoreView mScoreView;
	protected OwerMoreRepasswordView mRepasswordView;
	protected OwerMoreSuggestView mOwerMoreSuggestView;
	protected OwerMoreDownloadView mOwerMoreDownloadView;
	protected OwerMessageDetailView mOwerMessageDetailView;
	protected OwerMoreAbout mOwerMoreAbout;
	
	protected OwerFriendView mOwerFriendView;
	protected OwerCollectView mOwerCollectView;
	protected OwerShareView mOwerShareView;
	
	protected OwerFriendDetailView mOwerFriendDetailView;
	protected OwerAddFriendView mOwerAddFriendView;
	protected boolean isSetting;
	protected boolean isCustom;
	protected View _mainView;
	
	protected TextView appNum;
	protected TextView turNum;
	protected ImageView imgHeader;
	
	LoadDataBus mLoadData;
	String mUserName;
	
	public OwerMainBase(Context context) 
	{
		super(context);
		mContext = context;
		isSetting = false;
		isCustom = false;
		Account account = AccountMgr.readAccountInfo(mContext);
		mUserName = account.getUsername();
	}
	
	protected void addMainView(int paramInt)
	{
		_mainView = LayoutInflater.from(getContext()).inflate(paramInt, this);
	}
	
	public void EnterSystemMessageView(MessageInfo minfo)
	{
		SwitchView(OwerViewKey.ll_personal_message_detail, minfo);
	}
	
	protected void initButton()
	{
		TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.ower_title));
		mBtnBack = findViewById(R.id.login_btn_return);
		mBtnBack.setOnClickListener(new OnBackListener());
		
		mBtnRight = findViewById(R.id.login_top_right);
		mBtnRight.setOnClickListener(new OnRightListener());
		
		btnRefresh = (Button) rootLayout.findViewById(R.id.btn_nodata_reflash);
		btnSettings = (Button) rootLayout
				.findViewById(R.id.btn_nodata_settings);
		btnRefresh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				refreshData();
			}
		});
		btnSettings.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent("/");
				ComponentName comp = new ComponentName("com.android.settings",
						"com.android.settings.WirelessSettings");
				intent.setComponent(comp);
				intent.setAction("android.intent.action.VIEW");
				getContext().startActivity(intent);
			}
		});
	}
	
	class OnBackListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			onBackClick();
		}
	}
	
	class OnRightListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			onTopRightClick();
		}
	}
	
	@Override
	public boolean goBack() {
		if(isSetting)
			return super.goBack();
		
		if(rootViewId != currentViewId)
		{
			onBackClick();
			return true;
		}
		
		return super.goBack();
	}
	
	@Override
	public boolean doContextItemSelected(MenuItem item) {
		OwerBaseView base = getOwerBaseView(currentViewId);
		if(base != null)
		{
			return base.doContextItemSelected(item);
		}
		return true;
	}
	
	public void onBackClick()
	{
		CommonViewUtils.hideKeyboard((Activity)mContext);
	}
	
	protected void addSubView(int layoutId, int viewId)
	{
		View v = View.inflate(mContext, layoutId, null);
		v.setVisibility(View.VISIBLE);
		v.setId(viewId);
		rootLayout.addView(v);
	}
	
	protected void PopViewById(int id)
	{
		for (int i =  rootLayout.getChildCount() - 1; i >= 0; i--) 
		{
			View view = rootLayout.getChildAt(i);
			if (view.getId() == id)
			{
				rootLayout.removeViewAt(i);
				break;
			}
		}
	}
	
	public void SwitchCurrentView()
	{
		SwitchView(currentViewId);
	}
	
	public void loadInfo(int viewId)
	{
		if(R.id.ll_personal_info == viewId || R.id.ll_manage_personal_info == viewId)
		{	
			if(mLoadData != null)
				return;
			
			mLoadData = new LoadDataBus(mContext, this);
			String mMobile = SharedPreferencesUtil.getValue(mContext, SharedKey.Shared_Mobile_Key, "");
			mLoadData.loadData(mMobile);
		}
		
	}
	
	@Override
	public void onCompleted(boolean isSuccess, String desc) {
		if(isSuccess)
		{
			if(R.id.ll_personal_info == currentViewId)
			{
				myOwerView.onLoadSuccess();
			}
			else if(R.id.ll_manage_personal_info == currentViewId)
			{
				mMyManageView.onLoadSuccess();
			}
		}
		
		if(R.id.ll_personal_info == currentViewId || R.id.ll_manage_personal_info == currentViewId)
			mLoadData = null;
		
	}

	public void SwitchView(int id, Object param) {
		loadInfo(id);
		
		boolean isCreated = false;
		for (int i = 0; i < rootLayout.getChildCount(); i++) 
		{
			View view = rootLayout.getChildAt(i);
			if (view.getId() == id)
			{
				isCreated = true;
				view.setVisibility(View.VISIBLE);
			}
			else
				view.setVisibility(View.GONE);
		}
		
		if(!isCreated)
		{
			CreateViewById(id, param);
			bindView(id);
		}
		currentViewId = id;
		setTopByView(id);
		OwerBaseView obv = getOwerBaseView(id);
		if(obv != null)
		{
			String title = obv.getTitle();
			if(!StringUtil.isEmpty(title))
				TopViewUtil.updateTopTitle(this, R.id.login_top_title, title);
		}
	}
	public void SwitchView(int id) {
		SwitchView(id, null);
	}
	
	public void SwitchlayoutView(int viewId)
	{
		for (int i = 0; i < rootLayout.getChildCount(); i++) 
		{
			View view = rootLayout.getChildAt(i);
			if (view.getId() == viewId)
			{
				view.setVisibility(View.VISIBLE);
			}
			else
				view.setVisibility(View.GONE);
		}
	}
	
	protected void CreateSpView(int layoutId, int viewId)
	{
		for (int i = 0; i < rootLayout.getChildCount(); i++) 
		{
			View view = rootLayout.getChildAt(i);
			view.setVisibility(View.GONE);
		}
		
		addSubView(layoutId, viewId);
		currentViewId = viewId;
		setTopByView(viewId);
	}
	
	public void refreshData() {
		OwerBaseView baseView = getCurrentView();
		if(baseView != null)
		{
			baseView.refashData();
		}
	}
	
	protected void setTopByView(int viewId)
	{
		TopViewUtil.updateRightTitle(_mainView, R.id.login_top_right, "");
		TopViewUtil.setTopBackVisable(this, R.id.login_btn_return, View.VISIBLE);
		TopViewUtil.setRightVisable(this, R.id.login_top_right, View.INVISIBLE);
		View rightBtn = this.findViewById(R.id.login_top_right);
		rightBtn.setSelected(false);
		switch (viewId) {
		case OwerViewKey.ll_personal_friend_detail:
		case OwerViewKey.ll_personal_friend:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.ower_btn_friend));
			break;
		case R.id.ll_manage_personal_info:
			TopViewUtil.setTopBackVisable(this, R.id.login_btn_return, View.INVISIBLE);
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mUserName);
			break;
		case ManageKey.ll_manage_custom:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.manage_btn_custom));
			break;
		case OwerViewKey.ll_personal_more_about:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,"关于我们");
			break;
		case R.id.ll_custom_personal_info:
			TopViewUtil.setTopBackVisable(this, R.id.login_btn_return, View.INVISIBLE);
			TopViewUtil.setRightVisable(this, R.id.login_top_right, View.VISIBLE);
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,"客户管理");
			TopViewUtil.updateRight(this, R.id.login_top_right, R.drawable.custom_info_btn);
			break;
		case ManageKey.ll_manage_custom_info:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,"客户信息");
			break;
		case ManageKey.ll_manage_add_custom:
		case ManageKey.ll_manage_add_custom_result:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,"添加客户");
			break;
		case ManageKey.ll_manage_group:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.manage_btn_group));
			break;
		case ManageKey.ll_manage_group_info:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,"客户分组");
			break;
		case ManageKey.ll_manage_add_group:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,"添加群组");
			break;
		case ManageKey.ll_manage_salary:
		case ManageKey.ll_manage_salary_query:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.manage_btn_salary));
			break;
		case OwerViewKey.ll_personal_add_friend:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,"邀请好友");
			break;
		case OwerViewKey.ll_personal_collect:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.ower_btn_collect));
			break;
		case OwerViewKey.ll_personal_setting:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.ower_btn_setting));
			break;
		case OwerViewKey.ll_personal_share:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.ower_btn_share));
			break;
		case OwerViewKey.ll_personal_message:
		case OwerViewKey.ll_personal_message_detail:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.ower_btn_message));
			break;
		case OwerViewKey.ll_personal_more_repassword:
			TopViewUtil.setTopBackVisable(this, R.id.login_btn_return, View.VISIBLE);
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.ower_more_repassword));
			break;
		case OwerViewKey.ll_personal_more_suggest:
			TopViewUtil.setTopBackVisable(this, R.id.login_btn_return, View.VISIBLE);
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.ower_more_suggest));
			break;
		case OwerViewKey.ll_personal_more_score:
			TopViewUtil.setTopBackVisable(this, R.id.login_btn_return, View.VISIBLE);
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.ower_more_score));
			break;
		case OwerViewKey.ll_personal_more_download:
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.ower_more_update));
			break;
		case R.id.ll_personal_info:
			TopViewUtil.setTopBackVisable(this, R.id.login_btn_return, View.INVISIBLE);
			TopViewUtil.updateTopTitle(this, R.id.login_top_title,mContext.getResources().getString(R.string.ower_title));
			break;
		default:
			break;
		}
		
		if(OwerViewKey.ll_personal_friend == viewId)
		{
			//TopViewUtil.updateRightTitle((Activity)mContext, R.id.login_top_right, "邀请好友");
			TopViewUtil.updateRight(this, R.id.login_top_right, R.drawable.top_add_button);
			TopViewUtil.setRightVisable(this, R.id.login_top_right, View.VISIBLE);
		}
		else if(OwerViewKey.ll_personal_message == viewId)
		{
			TopViewUtil.updateRight(this, R.id.login_top_right, R.drawable.top_delete_button);
			TopViewUtil.setRightVisable(this, R.id.login_top_right, View.VISIBLE);
		}
		else if(OwerViewKey.ll_personal_collect == viewId)
		{
			TopViewUtil.updateRight(this, R.id.login_top_right, R.drawable.top_delete_button);
			TopViewUtil.setRightVisable(this, R.id.login_top_right, View.VISIBLE);
		}
		else if(ManageKey.ll_manage_custom == viewId)
		{
			TopViewUtil.updateRight(this, R.id.login_top_right, R.drawable.login_save_btn);
			TopViewUtil.updateRightTitle(_mainView, R.id.login_top_right, "排序");
			TopViewUtil.setRightVisable(this, R.id.login_top_right, View.VISIBLE);
			/*
			if(!isCustom)
			{
				TopViewUtil.updateRight(this, R.id.login_top_right, R.drawable.top_add_button);
				TopViewUtil.setRightVisable(this, R.id.login_top_right, View.VISIBLE);
				//TopViewUtil.updateRightTitle((Activity)mContext, R.id.login_top_right, "添加客户");
			}
			*/
		}
		else if(R.id.ll_personal_info == viewId || R.id.ll_manage_personal_info == viewId)
		{
			TopViewUtil.updateRight(this, R.id.login_top_right, R.drawable.login_save_btn);
			TopViewUtil.updateRightTitle(_mainView, R.id.login_top_right, "注销");
			TopViewUtil.setRightVisable(this, R.id.login_top_right, View.VISIBLE);
		}
		
		if(isSetting && OwerViewKey.ll_personal_setting == viewId)
		{
			TopViewUtil.setRightVisable(this, R.id.login_btn_return, View.INVISIBLE);
		}
	}
	
	protected void onTopRightClick()
	{
		if(OwerViewKey.ll_personal_friend == currentViewId)
		{
			doAddFriend();
		}
		else if(OwerViewKey.ll_personal_message == currentViewId)
		{
			doDeleteMessage();
		}
		else if(OwerViewKey.ll_personal_collect == currentViewId)
		{
			doDeleteCollect();
		}
		else if(R.id.ll_personal_info == currentViewId || R.id.ll_manage_personal_info == currentViewId)
		{
			AlertDialog.Builder renameDialog=new AlertDialog.Builder(mContext);
			renameDialog.setTitle("确认注销吗？")
			.setPositiveButton(R.string.common_button_confirm, new DialogInterface.OnClickListener() {	
				@Override
				public void onClick(DialogInterface dialog, int which) {
					OwerProvider.getInstance().loginOut(mContext);
					((CoachSiteMainActivity)mContext).refreshMainMenu();
				}
			})
			.setNegativeButton(R.string.common_button_cancell, new DialogInterface.OnClickListener() {	
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			})
			.create().show();	
			
		}
	}
	
	protected void sortCustomColumn(int index)
	{
		
	}
	
	private void doAddFriend()
	{
		SwitchView(OwerViewKey.ll_personal_add_friend);
	}
	
	protected void doDeleteCollect()
	{
		if(mOwerCollectView != null)
		{
			mOwerCollectView.setEditState();
		}
	}
	
	protected void doDeleteMessage()
	{
		if(mOwerMessageView != null)
		{
			mOwerMessageView.setEditState();
		}
	}
	
	protected void bindView(int id)
	{
		OwerBaseView view = getOwerBaseView(id);
		if(view != null)
		{
			view.setViewId(id);
		}
	}
	
	protected OwerBaseView getOwerBaseView(int viewId)
	{
		OwerBaseView baseView = null;
		switch (viewId) {
		case OwerViewKey.ll_personal_setting:
			baseView = mOwerSettingView;
			break;
		case OwerViewKey.ll_personal_message:
			baseView = mOwerMessageView;
			break;
		case OwerViewKey.ll_personal_message_detail:
			baseView = mOwerMessageDetailView;
			break;
		case OwerViewKey.ll_personal_more_repassword:
			baseView = mRepasswordView;
			break;
		case OwerViewKey.ll_personal_more_suggest:
			baseView = mOwerMoreSuggestView;
			break;
		case OwerViewKey.ll_personal_more_score:
			baseView = mScoreView;
			break;
		case OwerViewKey.ll_personal_more_about:
			baseView = mOwerMoreAbout;
			break;
		case OwerViewKey.ll_personal_more_download:
			baseView = mOwerMoreDownloadView;
			break;
		case OwerViewKey.ll_personal_friend:
			baseView = mOwerFriendView;
			break;
		case OwerViewKey.ll_personal_collect:
			baseView = mOwerCollectView;
			break;
		case OwerViewKey.ll_personal_friend_detail:
			baseView = mOwerFriendDetailView;
			break;
		case OwerViewKey.ll_personal_add_friend:
			baseView = mOwerAddFriendView;
			break;
		case OwerViewKey.ll_personal_share:
			baseView = mOwerShareView;
			break;
		}
		
		return baseView;
	}
	
	protected OwerBaseView getCurrentView()
	{
		return getOwerBaseView(currentViewId);
	}
	
	protected boolean CreateViewById(int Id, Object param)
	{	
		boolean isCreate = true;
		switch (Id) {
		case OwerViewKey.ll_personal_setting:
			addSubView(R.layout.ower_personal_setting, Id);
			if(mOwerSettingView != null)
				mOwerSettingView = null;
			mOwerSettingView = new OwerSettingView(mContext, this);
			break;
		case OwerViewKey.ll_personal_message:
			addSubView(R.layout.ower_personal_message, Id);
			if(mOwerMessageView != null)
				mOwerMessageView = null;
			mOwerMessageView = new OwerMessageView(mContext, this);
			break;
		case OwerViewKey.ll_personal_more_repassword:
			addSubView(R.layout.ower_personal_more_repassword, Id);
			if(mRepasswordView != null)
				mRepasswordView = null;
			mRepasswordView = new OwerMoreRepasswordView(mContext, this);
			break;
		case OwerViewKey.ll_personal_more_suggest:
			addSubView(R.layout.ower_personal_more_suggest, Id);
			if(mOwerMoreSuggestView != null)
				mOwerMoreSuggestView = null;
			mOwerMoreSuggestView = new OwerMoreSuggestView(mContext, this);
			break;
		case OwerViewKey.ll_personal_more_score:
			addSubView(R.layout.ower_personal_more_score, Id);
			if(mScoreView != null)
				mScoreView = null;
			mScoreView = new OwerMoreScoreView(mContext, this);
			break;
		case OwerViewKey.ll_personal_more_download:
			addSubView(R.layout.ower_personal_more_download, Id);
			if(mOwerMoreDownloadView != null)
				mOwerMoreDownloadView = null;
			mOwerMoreDownloadView = new OwerMoreDownloadView(mContext, this);
			break;
		case OwerViewKey.ll_personal_more_about:
			addSubView(R.layout.ower_personal_more_about, Id);
			if(mOwerMoreAbout != null)
				mOwerMoreAbout = null;
			mOwerMoreAbout = new OwerMoreAbout(mContext, this);
			break;
		case OwerViewKey.ll_personal_friend:
			addSubView(R.layout.ower_personal_friend, Id);
			if(mOwerFriendView != null)
				mOwerFriendView = null;
			mOwerFriendView = new OwerFriendView(mContext, this);
			break;
		case OwerViewKey.ll_personal_collect:
			addSubView(R.layout.ower_personal_collect, Id);
			if(mOwerCollectView != null)
				mOwerCollectView = null;
			mOwerCollectView = new OwerCollectView(mContext, this);
			break;
		case OwerViewKey.ll_personal_add_friend:
			addSubView(R.layout.ower_personal_add_friend, Id);
			if(mOwerAddFriendView != null)
				mOwerAddFriendView = null;
			mOwerAddFriendView = new OwerAddFriendView(mContext, this);
			break;
		case OwerViewKey.ll_personal_share:
			addSubView(R.layout.ower_personal_share, Id);
			if(mOwerShareView != null)
				mOwerShareView = null;
			mOwerShareView = new OwerShareView(mContext, this);
			break;
		case OwerViewKey.ll_personal_friend_detail:
			addSubView(R.layout.ower_personal_friend_detail, Id);
			if(mOwerFriendDetailView != null)
				mOwerFriendDetailView = null;
			mOwerFriendDetailView = new OwerFriendDetailView(mContext, this, (UserInfo)param);
			break;
		case OwerViewKey.ll_personal_message_detail:
			addSubView(R.layout.ower_personal_message_detail, Id);
			if(mOwerMessageDetailView != null)
				mOwerMessageDetailView = null;
			mOwerMessageDetailView = new OwerMessageDetailView(mContext, this, (MessageInfo)param);
			break;
			
		default:
			isCreate = false;
				break;
		}
		
		return isCreate;
	}
}
